var util = require('../../libs/util.js');
var app = getApp(), checkType;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    plate: '',
    vehicleId: '',
    checkType:'',
    form: {
      orderId: '',
      vehicleId: '',
      checkType: '',
      vehicle_fit: '',
      mile: '',
      oil_gauge: '',
      credentials: '',
      light: '',
      tire_pressure: '',
      tire_tread: '',
      cold_machine: '',
      bodywork: '',
      engine_oil: '',
      door_win: '',
      windshield: '',
      water_tank: '',
      glass_cleaner: '',
      brake: '',
      location:'',
      location_address:'',
      remark: ''
    },
    location_address:'请点击选择停车位置',
    selectNormal: [{ name: '正常', value: '1' }, { name: '异常', value: '0' }],
    selectYesNo: [{ name: '是', value: '1' }, { name: '否', value: '0' }],
    selectPlate: [{ name: '车辆符合', value: '1' }, { name: '车辆不符合', value: '0' }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    checkType = e.type;
    that.setData({
      id: e.id,
      plate: e.plate,
      vehicleId: e.vehicle,
      checkType:e.type
    });

    that.setData({
      'form.orderId': e.id,
      'form.vehicleId': e.vehicle,
      'form.checkType': e.type
    })
  },
  bindFormInput: function (e) {
    var that = this;
    var item = String(e.currentTarget.id), item_value = {};
    var form = that.data.form;
    form[item] = e.detail.value;
    that.setData({
      form: form
    });
  },
  handleTypeChange: function (e) {
    var that = this;
    var item = String(e.currentTarget.id), item_value = {};
    var form = that.data.form;
    form[item] = e.detail.value;
    that.setData({
      form: form
    });
    var data = that.data[item];
    console.log(data)
  },
  submitForm(e) {
    var that = this;
    var data = that.data.form;
    for (var f in data) {
      if (data[f] == '' && that.messages[f]['required']){
        wx.showModal({
          title: "温馨提示",
          content: that.messages[f]['msg'],
          success: function (res) {
            if (res.confirm) {
            }
          }
        });
        return false;
      }
    }
    util.postData("checkVehicle", data, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({ title: '提交失败', image: '/images/wrong.svg', duration: 2000 });
        return false;
      }
      if (res.data.success) {
        //轨迹
        wx.showModal({
          title: "温馨提示",
          content: res.data.data,
          success: function (res) {
            wx.redirectTo({
              url: '/pages/driver/currentOrder?id=' + that.data.id
            });
          }
        });
      } else {
        wx.showToast({ title: '提交失败', image: '/images/wrong.svg', duration: 2000 });
      };
    });
  },
  messages: {
    vehicle_fit: { 'required': true, msg: '请核实出车车辆是否一致' },
    mile: { 'required': true, msg: '请填写总公里数' },
    oil_gauge: { 'required': true, msg: '请填写油表的剩余油格或者剩余电量' },
    credentials: { 'required': true, msg: '请确认证件是否齐全，行驶证、驾驶证等' },
    light: { 'required': true, msg: '请检查车灯是否正常' },
    tire_pressure: { 'required': true, msg: '请检查胎压是否正常' },
    tire_tread: { 'required': true, msg: '请检查胎纹是否正常' },
    cold_machine: { 'required': true, msg: '请检查冷机是否制冷' },
    bodywork: { 'required': true, msg: '请检查车身是否异常' },
    engine_oil: { 'required': true, msg: '请检查机油是否足够' },
    door_win: { 'required': true, msg: '请检查门窗是否正常' },
    windshield: { 'required': true, msg: '请检查挡风玻璃是否正常' },
    water_tank: { 'required': true, msg: '请检查水箱的水是否足够' },
    glass_cleaner: { 'required': true, msg: '请检查雨刮水是否足够' },
    brake: { 'required': true, msg: '请检查刹车是否正常' },
    location: { 'required': checkType == 'afterCheck' ? true : false, msg: '请选择停车位置' },
    location_address: { 'required': false },
    remark: { 'required': false }
  },
  backToShopList: function () {
    wx.showModal({
      title: '温馨提示',
      content: '确认返回运单列表',
      success: function (res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/driver/orderList'
          })
        }
        if (res.cancel) {

        }
      }
    })
  },
  selectLocation:function(){
    var that = this;
    wx.chooseLocation({
      success: function(res){
        console.log(res)
        that.setData({
          location_address: res.address,
          'form.location_address': res.address,
          'form.location': res.latitude + ',' + res.longitude
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})